import { http } from '../utils/http/index'

// 教育经历
export interface Education {
  id: string
  userId: string
  school: string
  education: string
  major: string
  beginDate: string
  endDate: string
}

// 项目经历
export interface ProjectExp {
  id: string
  userId: string
  resumeId: string
  projectName: string
  roleName: string
  beginDate: string
  endDate: string
  content: string
  contentHtml: string
  projectAchievement: string
  projectAchievementHtml: string
  projectUrl: string
}

// 求职期望
export interface WorkExpect {
  id: string
  userId: string
  resumeId: string
  jobName: string
  city: string
  industry: string
  beginSalary: number
  endSalary: number
}

// 工作经历
export interface WorkExp {
  id: string
  userId: string
  resumeId: string
  companyName: string
  industry: string
  beginDate: string
  endDate: string
  position: string
  department: string
  content: string
  contentHtml: string
}

// 简历数据
export interface ResumeData {
  face: string
  nickName: string
  phone: string
  birthday: string
  id: string
  userId: string
  sex: number
  advantage: string
  advantageHtml: string
  credentials: string
  skills: string
  status: string
  refreshTime: string
  graduationYear: number
  educationList: Education[]
  projectExpList: ProjectExp[]
  workExpectList: WorkExpect[]
  workExpList: WorkExp[]
}

/**
 * 获取我的简历
 */
export async function getMyResume(): Promise<ResumeData> {
  const response = await http.get('/portal/resume/my_resume')
  return response as ResumeData
}

/**
 * 提交求职期望（新增或编辑）
 */
export async function submitWorkExpect(data: {
  id?: string
  resumeId: string
  jobName: string
  city: string
  industry: string
  beginSalary: number
  endSalary: number
}): Promise<void> {
  await http.post('/portal/resume/work_expect/submit', data)
}

/**
 * 删除求职期望
 */
export async function deleteWorkExpect(workExpectId: string): Promise<void> {
  await http.get(`/portal/resume/work_expect/del?workExpectId=${workExpectId}`)
}

/**
 * 修改简历信息（个人优势、求职状态、证书、技能等）
 */
export async function modifyResume(data: {
  advantage: string | null
  advantageHtml: string | null
  credentials: string | null
  skills: string | null
  status: string | null
}): Promise<void> {
  await http.post('/portal/resume/modify', data)
}

/**
 * 提交项目经历（新增或编辑）
 */
export async function submitProjectExp(data: {
  id?: string
  resumeId: string
  projectName: string
  roleName: string
  beginDate: string
  endDate: string
  content: string
  contentHtml: string
  projectAchievement: string
  projectAchievementHtml: string
  projectUrl: string
}): Promise<void> {
  await http.post('/portal/resume/proj_experience/submit', data)
}

/**
 * 删除项目经历
 */
export async function deleteProjectExp(projExperienceId: string): Promise<void> {
  await http.get(`/portal/resume/proj_exp/del?projExperienceId=${projExperienceId}`)
}

/**
 * 提交工作经历（新增或编辑）
 */
export async function submitWorkExp(data: {
  id?: string
  resumeId: string
  companyName: string
  industry: string
  beginDate: string
  endDate: string
  position: string
  department: string
  content: string
  contentHtml: string
}): Promise<void> {
  await http.post('/portal/resume/work_experience/submit', data)
}

/**
 * 删除工作经历
 */
export async function deleteWorkExp(workExperienceId: string): Promise<void> {
  await http.get(`/portal/resume/work_exp/del?workExperienceId=${workExperienceId}`)
}

/**
 * 提交教育经历（新增或编辑）
 */
export async function submitEducation(data: {
  id?: string
  resumeId: string
  school: string
  education: string
  major: string
  beginDate: string
  endDate: string
}): Promise<void> {
  await http.post('/portal/resume/education/submit', data)
}

/**
 * 删除教育经历
 */
export async function deleteEducation(educationId: string): Promise<void> {
  await http.get(`/portal/resume/education/del?educationId=${educationId}`)
}

/**
 * 获取我的简历预览（包含resumeExpId字段）
 */
export async function getMyResumePreview(): Promise<ResumeData> {
  const response = await http.get('/portal/resume/my_resume')
  return response as ResumeData
}

/**
 * 通过求职期望ID获取简历详情（HR查看）
 */
export async function getResumeByWorkExp(resumeExpId: string): Promise<ResumeData> {
  const response = await http.get(`/portal/resume/detail_by_work_exp?resumeExpId=${resumeExpId}`)
  return response as ResumeData
}

/**
 * 通过用户ID获取简历详情（HR查看）
 */
export async function getResumeByUserId(userId: string): Promise<ResumeData> {
  const response = await http.get(`/resume/detail?userId=${userId}`)
  return response as ResumeData
}

/**
 * 获取我的求职期望列表
 */
export async function getMyJobIntention(): Promise<{ workExpectList: WorkExpect[], status: string }> {
  const response = await http.get('/portal/resume/my_job_intention')
  return response as { workExpectList: WorkExpect[], status: string }
}

// 简历搜索请求参数
export interface ResumeSearchRequest {
  pageNo: number
  pageSize: number
  basicTitle?: string
  jobName?: string
  beginAge?: string
  endAge?: string
  sex?: number
  activeTime?: string
  workExpYears?: string
  edu?: string
  beginSalary?: number
  endSalary?: number
  jobStatus?: string
}

// 简历列表项
export interface ResumeListItem {
  resumeExpId: string
  userId: string
  realName: string
  face: string
  birthday: string
  resumeId: string
  skills: string
  advantage: string
  advantageHtml: string
  credentials: string
  status: string
  refreshTime: string
  highestEduLevel: string
  highestEduSchool: string
  highestEduMajor: string
  expectJobName: string
  expectBeginSalary: number
  expectEndSalary: number
  latestCompany: string
  latestPosition: string
  latestWorkBegin: string
  latestWorkEnd: string
  totalWorkYears: number
}

// 简历搜索响应
export interface ResumeSearchResponse {
  totals: number
  totalPages: number
  list: ResumeListItem[]
}

/**
 * HR搜索简历列表
 */
export async function searchResumes(params: ResumeSearchRequest): Promise<ResumeSearchResponse> {
  const response = await http.post('/portal/resume/hr_search', params)
  return response as ResumeSearchResponse
}

/**
 * HR查询是否已收藏简历
 */
export async function checkResumeCollected(resumeExpId: string): Promise<boolean> {
  const response = await http.get(`/portal/resume/hr_is_collected?resumeExpId=${resumeExpId}`, undefined, { showLoading: false })
  return response as boolean
}

/**
 * HR收藏简历
 */
export async function collectResume(resumeExpId: string): Promise<void> {
  await http.post(`/portal/resume/hr_collect?resumeExpId=${resumeExpId}`)
}

/**
 * HR取消收藏简历
 */
export async function uncollectResume(resumeExpId: string): Promise<void> {
  await http.post(`/portal/resume/hr_uncollect?resumeExpId=${resumeExpId}`)
}

/**
 * HR记录浏览简历（后台静默调用，不显示loading）
 */
export async function recordResumeView(resumeExpectId: string): Promise<void> {
  await http.post(`/portal/resume/hr_view_record?resumeExpectId=${resumeExpectId}`, undefined, { showLoading: false })
}

/**
 * HR获取收藏简历数量（后台加载）
 */
export async function getCollectedCount(): Promise<number> {
  const response = await http.get('/portal/resume/hr_collected/count', undefined, { showLoading: false })
  return response as number
}

/**
 * HR获取浏览简历数量（后台加载）
 */
export async function getViewCount(): Promise<number> {
  const response = await http.get('/portal/resume/hr_view/count', undefined, { showLoading: false })
  return response as number
}

/**
 * 求职者获取"谁看过我"数量（后台加载）
 */
export async function getViewedByHrCount(): Promise<number> {
  const response = await http.get('/portal/resume/job_seeker_viewed_by_hr/count', undefined, { showLoading: false })
  return response as number
}

/**
 * HR获取收藏简历列表（分页）
 */
export async function getCollectedResumeList(params: {
  pageNo: number
  pageSize: number
}): Promise<ResumeSearchResponse> {
  const response = await http.get(`/portal/resume/hr_collected/list?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as ResumeSearchResponse
}

/**
 * HR获取浏览记录列表（分页）
 */
export async function getViewedResumeList(params: {
  pageNo: number
  pageSize: number
}): Promise<ResumeSearchResponse> {
  const response = await http.get(`/portal/resume/hr_view/list?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as ResumeSearchResponse
}

// 谁看过我 - HR信息
export interface HrViewInfo {
  hrId: string
  face: string
  companyName: string
  position: string
  viewTime: string
}

// 谁看过我响应
export interface HrViewListResponse {
  totals: number
  totalPages: number
  list: HrViewInfo[]
}

/**
 * 求职者获取谁看过我列表（分页）
 */
export async function getViewedByHrList(params: {
  pageNo: number
  pageSize: number
}): Promise<HrViewListResponse> {
  const response = await http.get(`/portal/resume/job_seeker_viewed_by_hr/list?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as HrViewListResponse
}

/**
 * 简历刷新
 */
export async function refreshResume(): Promise<void> {
  await http.post('/portal/resume/refresh')
}


